package com.itheima.controller.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

/**
 * @author Xiao
 * @date 2021/9/16
 */
@Component
public class ProjectInterceptor2 implements HandlerInterceptor {
    //①preHandle
    //request --> 请求对象
    //response --> 响应对象
    //handler --> 被调用的处理器对象，本质上是一个方法对象，对反射技术中的Method对象进行了再包装
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("preHandle...2");
        // false --> 终止原始操作，即①之后的所有操作都不会执行
        return false;
    }

    //②：controller代码

    //③postHandle
    //modelAndView --> 封装了SpringMvc页面跳转的相关数据
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle...2");
    }

    //④afterCompletion
    //ex --> 获取表现层的异常对象进行处理
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion...2");
    }
}
